﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using Bzure.Common.Core;

namespace Bzure.Security.Permissions
{
   internal static class RolePool
    {
        static Dictionary<string, TeamRole> teamRoles = new Dictionary<string, TeamRole>();
        static Dictionary<string, TreeRole> treeRoles = new Dictionary<string, TreeRole>();
        static RolePool()
        {
            InitTreamRolePool();
            InitTreeRolePool();
        }
        /// <summary>
        /// 重建TeamRole数据池
        /// </summary>
        public static void InitTreamRolePool()
        {
            IRoleContract provider = Actor.Public.BuildContractProvider<IRoleContract>();
            if (provider != default(IRoleContract))
            {
                teamRoles.Clear();
                TeamRole[] trs = provider.GetAllTeamRoles();
                foreach (TeamRole tr in trs)
                {
                    tr.AddDroit(provider.GetDroitsByRole(tr));
                    teamRoles[tr.ID] = tr;
                }
            }
        }
        /// <summary>
        /// 重建TreeRole数据池
        /// </summary>
        public static void InitTreeRolePool()
        {
        }
        public static TreeRole GetTreeRolesByUser(string UserID)
        {
            return null;
        }
        public static TeamRole[] GetTeamRolesByUser(string UserID)
        {
            IRoleContract provider = Actor.Public.BuildContractProvider<IRoleContract>();
            if (provider != default(IRoleContract))
            {
                List<TeamRole> list = new List<TeamRole>();
                foreach (string roleID in provider.GetTeamRoleIDsByUserID(UserID))
                {
                    TeamRole tr;
                    if (teamRoles.TryGetValue(roleID, out tr))
                    {
                        list.Add(tr);
                    }
                }
                return list.ToArray();
            }
            return default(TeamRole[]);
        }
    }
}
